/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Compresor;

import java.io.Serializable;
import java.util.BitSet;

/**
 *
 * @author asc
 */
public class BitSetUtil extends BitSet implements Serializable
{
    private int contador;
    public BitSetUtil(){
        super();
        contador=0;
    }

    public BitSetUtil(int i)
    {
        super(i);
    }

    @Override
    public boolean equals(Object obj)
    {
        if(obj.getClass().getName().equals("Compresor.BitSetUtil"))
        {
            BitSetUtil b=(BitSetUtil) obj;
            if(this.contador == b.contador)
            {
                return super.equals(b);
            }
        }
        return false;
    }

    public void set(boolean bit)
    {
        super.set(contador , bit);
        contador++;
    }

    @Override
    public String toString()
    {
        String Salida="";
        for(int i=0; i<contador; i++)
        {
            Salida+=(get(i))?"1":"0";
        }
        return Salida;
    }
    
    public void addBitSet(BitSetUtil x)
    {
        String tmp = x.toString();
        for(int i=0; i<tmp.length(); i++)
        {
            if(tmp.charAt(i) == '1')
            {
                this.set(true);
            }
            else
            {
                this.set(false);
            }
        }
    }

    @Override
    public int hashCode()
    {
        int hash = 7;
        return hash;
    }

    @Override
    public int size()
    {
        return contador;
    }
    
    public static BitSet fromByte(byte b)
    {
        BitSet bits = new BitSet(8);
        for (int i = 0; i < 8; i++)
        {
            bits.set(i, (b & 1) == 1);
            b >>= 1;  
        }
        return bits;
    }
}
